﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BoolToVisibilityConverter.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   Converter class for converting a bool Value to a Visibility Value, the target values of the conversion are settable
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.Controls.ConvertersAndTemplateSelectors
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Markup;

    /// <summary>
    ///   Converter class for converting a bool Value to a Visibility Value, the target values of the conversion are settable
    /// </summary>
    public class BoolToVisibilityConverter : MarkupExtension, IValueConverter
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BoolToVisibilityConverter"/> class. 
        /// constructor with default conversion behaviour (false=&gt;Visibility.Collapsed, true=&gt;Visibility.Visible)
        /// </summary>
        public BoolToVisibilityConverter()
        {
            this.TrueValue = Visibility.Visible;
            this.FalseValue = Visibility.Collapsed;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Target-value of the false-conversion
        /// </summary>
        public Visibility FalseValue { get; set; }

        /// <summary>
        ///   Target-value of the true-conversion
        /// </summary>
        public Visibility TrueValue { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// conversion metdod
        /// </summary>
        /// <param name="value">
        /// </param>
        /// <param name="targetType">
        /// </param>
        /// <param name="parameter">
        /// </param>
        /// <param name="culture">
        /// </param>
        /// <returns>
        /// The System.Object. 
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var val = System.Convert.ToBoolean(value);
            return val ? this.TrueValue : this.FalseValue;
        }

        /// <summary>
        /// Backwards conversion
        /// </summary>
        /// <param name="value">
        /// </param>
        /// <param name="targetType">
        /// </param>
        /// <param name="parameter">
        /// </param>
        /// <param name="culture">
        /// </param>
        /// <returns>
        /// The System.Object. 
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return this.TrueValue.Equals(value);
        }

        /// <summary>
        /// Value Provider
        /// </summary>
        /// <param name="serviceProvider">
        /// </param>
        /// <returns>
        /// The System.Object. 
        /// </returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        #endregion
    }
}